package com.hyacinth.cloudnote.modules.note.service;

import com.hyacinth.cloudnote.common.exception.ExceptionHolder;
import com.hyacinth.cloudnote.modules.note.model.Note;
import com.hyacinth.cloudnote.modules.note.model.NoteBook;
import com.hyacinth.cloudnote.modules.note.model.NoteTag;
import com.hyacinth.cloudnote.modules.note.model.Tag;
import com.hyacinth.cloudnote.modules.note.repository.NoteRepository;
import com.hyacinth.cloudnote.modules.note.repository.NoteTagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.UUID;

import static com.hyacinth.cloudnote.modules.common.errorcode.NoteErrorCode.E1000000001;

/**
 * Note实体对应的业务逻辑Service实现类
 * @author zhangfb
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class NoteService {

    @Autowired
    private NoteRepository noteRepository;
    @Autowired
    private NoteTagRepository noteTagRepository;
    @Autowired
    private TagService tagService;
    @Autowired
    private NoteBookService noteBookService;
    @Autowired
    private FavoriteService favoriteService;

    public void save(Note model, List<Tag> tags) {
        // 保存标签
        if (!CollectionUtils.isEmpty(tags)) {
            // 删除NoteTag
            noteTagRepository.deleteAllByNote(model);
            tags.forEach(tag -> {
                tag.setNoteNum(tag.getNoteNum() + 1);
                tagService.save(tag);
                noteTagRepository.save(new NoteTag(model, tag));
            });
        }
        if (model.isNew()) {
            // 笔记本中笔记数量+1
            NoteBook noteBook = model.getNoteBook();
            noteBook.setNoteNum(noteBook.getNoteNum() + 1);
            noteBookService.save(noteBook);
        }
        // 保存笔记
        noteRepository.save(model);
    }

    public void disable(UUID id) {
        Note note = noteRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "笔记-" + id));
        // 删除笔记
        note.setDisable(true);
        noteRepository.save(note);

    }

    public Note reduce(UUID id) {
        Note note = noteRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "笔记-" + id));
        note.setDisable(false);
        return noteRepository.save(note);
    }

    public void delete(UUID id) {
        Note note = noteRepository.findById(id)
                .orElseThrow(() -> ExceptionHolder.serviceException(E1000000001, "笔记-" + id));
        // 更新标签数量-1
        List<NoteTag> noteTags = noteTagRepository.findByNote(note);
        if (!CollectionUtils.isEmpty(noteTags)) {
            noteTags.forEach(noteTag -> {
                Tag tag = noteTag.getTag();
                tag.setNoteNum(tag.getNoteNum() - 1);
                tagService.save(tag);
            });
        }
        // 更新笔记本笔记数量
        NoteBook noteBook = note.getNoteBook();
        noteBook.setNoteNum(noteBook.getNoteNum() - 1);
        noteBookService.save(noteBook);

        // 删除笔记收藏记录
        favoriteService.deleteByNote(note);
        // 删除NoteTag
        noteTagRepository.deleteAllByNote(note);
        noteRepository.delete(note);
    }

}